<?php
namespace home\controller;
use framework\core\Controller;
use framework\core\Factory;
class QuestionController extends Controller{
    //显示添加问题页面
    public function addAction(){
        $this->smarty->left_delimiter="<{";
        $this->smarty->right_delimiter="}>";
        //显示所有分类
        $model=Factory::M("admin\model\CategoryModel");
        $cat_list=$model->category_select();
        $this->smarty->assign("cat_list",$cat_list);
        //显示所有标题
        $t_model=Factory::M('admin\model\TopicModel');
        $topics=$t_model->getAllTopics();
        $this->smarty->assign("topics",$topics);
        $this->smarty->display("question/add.html");
    }
    public function addHandleAction(){
        $data['question_title']=$_POST['question_content'];
        $data['question_desc']=$_POST['question_desc'];
        $data['user_id']=1;//默认的用户
        $data['pub_time']=time();
        $data['is_pass']=0;//默认没有审核通过的
        $q_model=Factory::M('QuestionModel');
        $res=$q_model->checkData($data);
        if ($res){
            $question_id=$q_model->insert($data);
            if (isset($_POST['topic_id'])){
                $tq_model=Factory::M("TopicQuestionModel");
                foreach ($_POST['topic_id'] as $k=>$v){
                    $dd['topic_id']=$v;
                    $dd['question_id']=$question_id;
                    $tq_id=$tq_model->insert($dd);
                }
            }
            if ($question_id){
                $this->jump('发起问题成功','index.php?m=home&c=Category&a=index');
            }
        }else{
            $this->jump('数据不合法,错误的原因是'.$q_model->getError(),
                'index.php?m=home&c=Question&a=add');
        }
    }
}